//
//  NSObject+ViewController.m
//  tesDemo02
//
//  Created by 拓客营 on 2022/3/21.
//

#import "NSObject+ViewController.h"

@implementation NSObject (ViewController)

+ (UIViewController *)jsd_getRootViewController{

    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    return window.rootViewController;
}

+ (UIViewController *)getCurrentViewController
{
    UIViewController* currentVC = [self jsd_getRootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentVC.presentedViewController) {

            currentVC = currentVC.presentedViewController;
        } else if ([currentVC isKindOfClass:[UINavigationController class]]) {

          UINavigationController* navigationController = (UINavigationController* )currentVC;
            currentVC = [navigationController.childViewControllers lastObject];

        } else if ([currentVC isKindOfClass:[UITabBarController class]]) {

          UITabBarController* tabBarController = (UITabBarController* )currentVC;
            currentVC = tabBarController.selectedViewController;
        } else {

            NSUInteger childViewControllerCount = currentVC.childViewControllers.count;
            if (childViewControllerCount > 0) {

                currentVC = currentVC.childViewControllers.lastObject;

                return currentVC;
            } else {

                return currentVC;
            }
        }

    }
    return currentVC;
}


@end
